#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/5/12 9:44
@File: 风险等级查询_查询.py
"""
import time
import random

from qturboframe.lib.utils.by import By
from qturboframe.lib.utils.variable import Variable

from BladeFerm.Lib.Common import data_processing
from BladeFerm.hui2_app import FE
from 基础公共脚本.公共方法_hui2 import open_menu, arm_form_fill
from 基础公共脚本.步骤检查_hui2 import check_table, check_ele_text, check_ele_no_text


@data_processing
def product_risk_review_search(input_data=None, ui_expect=None):
    
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': input_data.pop("菜单名称")})

    # 搜索框表单填入
    arm_form_fill(input_data)

    # 检查查询结果
    check_table(input_data=None, ui_expect=ui_expect)           # 此方法待完成


@data_processing
def product_risk_review_history(input_data=None, ui_expect=None):
    
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': input_data.pop("菜单名称")})

    # 表格点击
    table = FE.table_new()
    table.click_cell(input_data, {"操作": "历史评级"})

    # 检查查询结果
    check_ele_text(input_data=None, ui_expect=ui_expect)           # 此方法待完成


@data_processing
def product_risk_review_add(input_data=None, ui_expect=None):
    
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': input_data.pop("菜单名称")})

    # 点击 新增
    FE.button("新增产品").click()
    FE.button(idname={'xpath': f'.//li[@class="h-dropdown-item"]/span[contains(text(),"单个新增")]'}).click()

    # 填写表单
    ext = str(random.randint(0, 100))
    product_name = input_data.get('产品名称', '')
    product_code = input_data.get('产品代码', '')
    if product_name and product_name.endswith("**"):
        product_name = product_name.replace("**", ext)
        dic = {'产品名称': product_name}
        input_data.update(dic)
        ui_expect.update(dic)
    if product_code and product_code.endswith("**"):
        product_code = product_code.replace("**", ext)
        dic2 = {'产品代码': product_code.replace("**", ext)}
        input_data.update(dic2)
        ui_expect.update(dic2)
    idname = {'xpath': '//div[@class="h-modal-header"]/div[contains(text(),"新增产品信息")]//'
                       'ancestor::div[@class="h-modal-content"]/div[@class="h-modal-body"]/form'}
    form = FE.form(idname=idname)
    form.auto_fill_group(input_data)

    # 点击提交
    express = f'//div[@class="h-modal-footer"]//button/span[contains(text(),"提交")]//parent::button'
    FE.button(idname={By.XPATH: express}).click()

    # 检查查询结果
    time.sleep(1)
    table = FE.table_new()
    table.check_expect_row(ui_expect, 1)

    # 设置返回变量
    variable = Variable()
    variable.set_variable('varc_product_name', product_name)
    variable.set_variable('varc_product_code', product_code)


@data_processing
def product_risk_review_modify(input_data=None, ui_expect=None):
    
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': input_data.pop("菜单名称")})

    # 表格点击
    table = FE.table_new()
    table.click_cell(input_data.get("修改目标"), {"操作": "修改"})

    # 填写表单
    idname = {'xpath': '//div[@class="h-modal-header"]/div[contains(text(),"修改产品信息")]//'
                       'ancestor::div[@class="h-modal-content"]/div[@class="h-modal-body"]/form'}
    form = FE.form(idname=idname)
    form.auto_fill_group(input_data.get("修改内容"))

    # 点击提交
    express = f'//div[@class="h-modal-footer"]//button/span[contains(text(),"提交")]//parent::button'
    FE.button(idname={By.XPATH: express}).click()

    # 检查查询结果
    check_ele_no_text(input_data=None, ui_expect=ui_expect)


@data_processing
def product_risk_review_delete(input_data=None, ui_expect=None):
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': input_data.pop("菜单名称")})

    # 表格点击
    table = FE.table_new()
    table.select_row(input_data)

    # 点击 新增
    FE.button("删除").click()

    # 点击确认弹框
    if ui_expect.pop("允许删除") == "是":
        click_ele = {"xpath": f'//div[@class="h-modal-confirm-footer"]/button/span[contains(text(),"确定")]'}
        FE.msg_box("确定要删除选中的数据").click_button(idname=click_ele)

    # 检查查询结果
    check_ele_no_text(input_data=None, ui_expect={"期望结果": ui_expect.get("检查名称")})


@data_processing
def product_risk_review_start_flow(input_data=None, ui_expect=None):
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': input_data.pop("菜单名称")})

    # 表格点击
    table = FE.table_new()
    table.select_row(input_data)

    # 点击 新增
    FE.button("发起评定").click()

    # 检查查询结果
    time.sleep(1)
    table.check(input_data, ui_expect)